package com.chookss.home.audit.clockIn;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chookss.R;
import com.chookss.home.entity.ClockInRulesEntity;
import com.chookss.tools.Utils;
import com.chookss.tools.address.BaseDialog;
import com.chookss.tools.address.SingleClick;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2019/02/12
 * desc   : 省市区选择对话框
 * doc    : https://baijiahao.baidu.com/s?id=1615894776741007967
 */
public final class RulesDialog {

    public static final class Builder
            extends BaseDialog.Builder<Builder>
            implements Runnable, BaseDialog.OnShowListener, BaseDialog.OnDismissListener {

        private final TextView tvTime, tvLate, tvAbsenteeism, tvLeaveEarly, tvLoc, tvSure,tvTitle;
        private final LinearLayout llTime, llLate, llAbsenteeism, llLeaveEarly, llLoc;
        @Nullable
        private OnListener mListener;


        @SuppressWarnings("all")
        public Builder(Context context) {
            super(context);
            setContentView(R.layout.rules_dialog);
            tvTitle = findViewById(R.id.tvTitle);
            tvTime = findViewById(R.id.tvTime);
            tvLate = findViewById(R.id.tvLate);
            tvAbsenteeism = findViewById(R.id.tvAbsenteeism);
            tvLeaveEarly = findViewById(R.id.tvLeaveEarly);
            tvLoc = findViewById(R.id.tvLoc);
            llTime = findViewById(R.id.llTime);
            llLate = findViewById(R.id.llLate);
            llAbsenteeism = findViewById(R.id.llAbsenteeism);
            llLeaveEarly = findViewById(R.id.llLeaveEarly);
            llLoc = findViewById(R.id.llLoc);
            tvSure = findViewById(R.id.tvSure);
            setOnClickListener(tvSure);
            addOnShowListener(this);
            addOnDismissListener(this);
        }

        public Builder setData(ClockInRulesEntity clockInRulesEntity,String title) {
            if (Utils.isNull(title)) {
                tvTitle.setText("考勤规则");
            }else{
                tvTitle.setText(title);
            }
            for (int i = 0; i < clockInRulesEntity.getKaoqinRules().size(); i++) {
//                '规则类型1:迟到,2:下班旷工,3:早退,4:外勤,5:补卡次数,6:延迟',
                if (i == 0) {
                    if (!Utils.isNull(clockInRulesEntity.getKaoqinRules().get(0).getStartTime())) {
                        llTime.setVisibility(View.VISIBLE);
                        tvTime.setText(clockInRulesEntity.getKaoqinRules().get(0).getStartTime() + "-" + clockInRulesEntity.getKaoqinRules().get(0).getEndTime());
                    }
                }
                if (clockInRulesEntity.getKaoqinRules().get(i).getRuleType().equals("1")) {
                    llLate.setVisibility(View.VISIBLE);
                    tvLate.setText("超过上班时间" + clockInRulesEntity.getKaoqinRules().get(i).getRuleData() + "分钟内打卡");
                } else if (clockInRulesEntity.getKaoqinRules().get(i).getRuleType().equals("2")) {
                    llAbsenteeism.setVisibility(View.VISIBLE);
                    tvAbsenteeism.setText("超过上班时间" + clockInRulesEntity.getKaoqinRules().get(i).getRuleData() + "分钟后打卡\n"+"未到下班时间" + clockInRulesEntity.getKaoqinRules().get(i).getRuleData() + "分钟前打卡");
                } else if (clockInRulesEntity.getKaoqinRules().get(i).getRuleType().equals("3")) {
                    llLeaveEarly.setVisibility(View.VISIBLE);
                    tvLeaveEarly.setText("未到下班时间前" + clockInRulesEntity.getKaoqinRules().get(i).getRuleData() + "分钟内打卡");
                }
            }
            if (!Utils.isNull(clockInRulesEntity.getAddressName())) {
                llLoc.setVisibility(View.VISIBLE);
                String addressName = "";
                String[] addressnNames = clockInRulesEntity.getAddressName().split(",");
                for (int i = 0; i < addressnNames.length; i++) {
                    if (i == 0) {
                        addressName = addressnNames[i];
                    } else {
                        addressName = addressName + "\n" + addressnNames[i];
                    }
                }
                tvLoc.setText(addressName);
            }
            return this;
        }

        public Builder setListener(OnListener listener) {
            mListener = listener;
            return this;
        }

        @Override
        public void run() {
            if (isShowing()) {
                dismiss();
            }
        }

        @SingleClick
        @Override
        public void onClick(View view) {
            if (view == tvSure) {
                dismiss();
                if (mListener == null) {
                    return;
                }
                mListener.onCancel(getDialog());
            }
        }

        @Override
        public void onShow(BaseDialog dialog) {

        }

        @Override
        public void onDismiss(BaseDialog dialog) {

        }
    }

    public interface OnListener {
        /**
         * 点击取消时回调
         */
        default void onCancel(BaseDialog dialog) {
        }
    }
}